using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using VoteMeDefine;


namespace VoteMe
{
    public partial class FolderChoice : Form
    {
        public FolderChoice()
        {
            InitializeComponent();
        }

        private void FolderChoice_Load(object sender, EventArgs e)
        {
            TVFolder.Nodes.Clear();
            DirectoryInfo dirRoot = new DirectoryInfo(@"\");
            TreeNode nodeRoot = new TreeNode();
            nodeRoot.Text =@"\";
            TVFolder.Nodes.Add(nodeRoot);
            //Add level 1 sub folder
            foreach(DirectoryInfo dirSub1 in dirRoot.GetDirectories())
            {
                if (dirSub1.ToString() == @"Windows")
                    goto Break;
                TreeNode nodeSub1 = new TreeNode();
                nodeSub1.Text = dirSub1.ToString();
                nodeRoot.Nodes.Add(nodeSub1);
                //Add level 2 sub folder
                foreach (DirectoryInfo dirSub2 in dirSub1.GetDirectories())
                {
                    TreeNode nodeSub2 = new TreeNode();
                    nodeSub2.Text = dirSub2.ToString();
                    nodeSub1.Nodes.Add(nodeSub2); 
                    //Add level 3 sub folder
                    foreach (DirectoryInfo dirSub3 in dirSub2.GetDirectories())
                    {
                        TreeNode nodeSub3 = new TreeNode();
                        nodeSub3.Text = dirSub3.ToString();
                        nodeSub2.Nodes.Add(nodeSub3);
                    }
                }
            Break:
                Application.DoEvents();
            }
            nodeRoot.Expand();
            
        }

        private void BtnDone_Click(object sender, EventArgs e)
        {
            string strTmpPicPath;
            try
            {

                strTmpPicPath = TVFolder.SelectedNode.FullPath.Remove(1, 1);
            }
            catch
            {
                strTmpPicPath = @"\";
            }
            

            //write picture location to ini file;
            string str;
            using (StreamReader sr = File.OpenText(VariableDefine.iniPath))
            {
                str = sr.ReadToEnd();
                str = str.Replace(@"[Pictures Location]=" + VariableDefine.picturePath, @"[Pictures Location]=" + strTmpPicPath);
                sr.Close();
            }
            
            using (StreamWriter sw=new StreamWriter(VariableDefine.iniPath,false,Encoding.UTF8))
            {
                sw.Write(str);
                sw.Close();
            }

            VariableDefine.picturePath = strTmpPicPath;
            //close this form and open main form.
            FrmMain frmMain = new FrmMain();
            frmMain.Show();
            this.Close();

        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            FrmMain frmMain=new FrmMain();
            frmMain.Show();
            this.Close();
        }

    }
}